import { app, BrowserWindow, Menu } from 'electron'
import { release } from 'node:os'
import { join } from 'node:path'
import {clear, createWindow,focus} from './main'
import { createTray } from "./tray";
import { logger } from "../util/log";
// The built directory structure
//
// ├─┬ dist-electron
// │ ├─┬ main
// │ │ └── index.js    > Electron-Main
// │ └─┬ preload
// │   └── index.js    > Preload-Scripts
// ├─┬ dist
// │ └── index.html    > Electron-Renderer
//
process.env.DIST_ELECTRON = join(__dirname, '..')
process.env.DIST = join(process.env.DIST_ELECTRON, '../dist')
process.env.VITE_PUBLIC = process.env.VITE_DEV_SERVER_URL
  ? join(process.env.DIST_ELECTRON, '../public')
  : process.env.DIST
// Disable GPU Acceleration for Windows 7
if (release().startsWith('6.1')) app.disableHardwareAcceleration()

// Set application name for Windows 10+ notifications
if (process.platform === 'win32') app.setAppUserModelId(app.getName())

if (!app.requestSingleInstanceLock()) {
  app.quit()
  process.exit(0)
}


app.whenReady().then(async () => {
  //隐藏菜单栏
  Menu.setApplicationMenu(null)
  createTray()
  await createWindow()
})

app.on('window-all-closed', () => {
  logger.info("main %s","window-all-closed")
  clear()
  // if (process.platform !== 'darwin') app.quit()
})

app.on('second-instance', async () => {
  logger.info("main %s","window-all-closed")
  await focus()
})

app.on('activate', async () => {
  const allWindows = BrowserWindow.getAllWindows()
  if (allWindows.length) {
    allWindows[0].focus()
  } else {
    await createWindow()
  }
})

